﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using com.tiyma.components;
using com.tiyma.common;
using com.tiyma.model;


namespace com.tiyma.web
{
    public partial class S : BasePage
    {
        const string m_template="<li class=\"clearfix\"><h3><a style=\"color: #0000ff\" target=\"_blank\" href=\"{0}\">"+
                                "{1}</a> / <a style=\"color: #0000ff\" href=\"{9}\">{2}</a> / <a style=\"color: #0000ff\" href=\"{3}\">{4}</a></h3>" +
                              
                                "<div class=\"clear\"></div>"+
                                "<dl class=\"clearfix\"> <dt>简&nbsp;&nbsp;&nbsp;&nbsp;介：</dt><dd>{5}</dd></dl>"+
                                "<div class=\"clear\"></div>"+
                                "<dl class=\"clearfix\"><dt>最新更新：</dt><dd><a target=\"_blank\" href=\"{6}\">{7}</a></dd></dl>"+
                                "<dl class=\"clearfix\"><dt>更新时间：</dt><dd>{8}</dd></dl><div class=\"clear\"></div></li>";


        const string m_template_seo = "<li class=\"clearfix\"><h3><a style=\"color: #0000ff\" target=\"_blank\" href=\"{0}\">" +
                                "{1}</a></h3>" +

                                "<div class=\"clear\"></div>" +
                                "<dl class=\"clearfix\"> <dt>简&nbsp;&nbsp;&nbsp;&nbsp;介：</dt><dd>{2}</dd></dl>" +
                                "<div class=\"clear\"></div>" +
                                "<dl class=\"clearfix\"><dt>更新时间：</dt><dd>{3}</dd></dl><div class=\"clear\"></div></li>";

        const string m_cur_read = "<li><a href=\"/book/ChapterList.aspx?bookId={0}&siteId={1}\" class=\"book\" target=\"_blank\">{2}</a> <a href=\"/book/Chapter.aspx?bookId={0}&chapterId={3}&siteId={1}\" class=\"chapter\" target=\"_blank\">{4}</a></li>";

        public string m_cur_read_list = string.Empty;

        public string m_data_list = string.Empty;

        public string m_page_list = string.Empty;

        public string m_key_word = string.Empty;


        int m_search_type = 4;
        int m_page_index = 1;
        int m_page_size = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
             m_key_word = Request["k"];

            if (!int.TryParse(Request["p"], out m_page_index))
                m_page_index = 1;

            if (!int.TryParse(Request["t"], out m_search_type))
                m_search_type = 4;

            BindSearchList(m_key_word);

            BindCurReadList();
        }

        private void BindCurReadList()
        {
            try
            {
                List<BookInfo> t_list = CookieManager.GetReadBookListByCookie();

                if (t_list != null && t_list.Count > 0)
                {
                    StringBuilder t_sb = new StringBuilder();

                    foreach (BookInfo t_book in t_list)
                    {
                        t_sb.AppendFormat(m_cur_read, t_book.BookId, t_book.SiteId, t_book.BookName, t_book.LastUpdateChapterId, t_book.LastUpdateChapterName);
                    }

                    m_cur_read_list = string.Format("<div id=\"right\"><div class=\"title\">▶最近阅读</div><ul>{0}</ul></div>", t_sb.ToString());
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定最近阅读失败" + ex.ToString());
            }
        }

        private void BindSearchList(string i_word)
        {
            BookManager t_book_mgr=new BookManager();

            Consts.SearchType t_search_type = Consts.SearchType.General;

            if (m_search_type == 1)
                t_search_type = Consts.SearchType.BookName;
            if (m_search_type == 2)
                t_search_type = Consts.SearchType.AuthorName;
            if (m_search_type == 3)
                t_search_type = Consts.SearchType.Category;

            try
            {
                int o_total_count=0;

                List<BookInfo> t_list = t_book_mgr.SearchBook(i_word,t_search_type, m_page_index, m_page_size, out o_total_count);

                //从自己库存搜索到了小说信息
                if (t_list != null && t_list.Count > 0)
                {
                    StringBuilder t_sb = new StringBuilder();

                    foreach (BookInfo t_book in t_list)
                    {
                        t_sb.AppendFormat(m_template, string.Format("/book/chapterList.aspx?bookId={0}&siteId={1}&b={2}&a={3}", t_book.BookId, t_book.SiteId,HttpUtility.UrlEncode(t_book.BookName),HttpUtility.UrlEncode(t_book.AuthorName)), GetHightWord(string.Format("{0}，{1}", t_book.BookName, t_book.SiteName), i_word), GetHightWord(t_book.AuthorName, i_word), string.Format("/book/S.aspx?k={0}&t={1}", HttpUtility.UrlEncode(t_book.BookCategory), (int)Consts.SearchType.Category),
                           GetHightWord(t_book.BookCategory, i_word),
                           SystemUtil.GetSubBookDescription(t_book.Description, i_word, 200), string.Format("/book/chapter.aspx?bookId={0}&chapterId={1}&siteId={2}&n={3}", t_book.BookId, t_book.LastUpdateChapterId, t_book.SiteId,HttpUtility.UrlEncode(t_book.BookName)), t_book.LastUpdateChapterName, t_book.LastUpdateTime.ToString("yyyy-MM-dd"), string.Format("/book/S.aspx?k={0}&t={1}", HttpUtility.UrlEncode(t_book.AuthorName), (int)Consts.SearchType.AuthorName));
                    }

                    m_data_list = t_sb.ToString();

                    if (o_total_count > m_page_size)
                    {
                        m_page_list = GetPageHtml(m_page_index, m_page_size, o_total_count, string.Format("S.aspx?k={0}&t={1}&p=", HttpUtility.UrlEncode(i_word),m_search_type));
                    }
                }
                    //从搜索引擎搜索小说信息
                else
                {
                    SEOManager t_seo_mgr = new SEOManager();

                    t_list = t_seo_mgr.BaiDuSearch(i_word);

                    if (t_list != null)
                    {
                        StringBuilder t_sb = new StringBuilder();
                        foreach (BookInfo t_book in t_list)
                        {
                            t_sb.AppendFormat(m_template_seo, t_book.BookChapterListUrl,t_book.BookName,t_book.Description,t_book.LastUpdateTime);
                        }

                        m_data_list = t_sb.ToString();
                    }
                }

              

            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定搜索结果失败" + ex.ToString());
            }
        }

        private string GetHightWord(string i_text,string i_word)
        {
            return i_text.Replace(i_word, string.Format("<b>{0}</b>", i_word));
        }
    }
}
